/*
 * @Author: andy.dell andy@long.com
 * @Date: 2024-01-07 21:37:44
 * @LastEditors: andy.dell andy@long.com
 * @LastEditTime: 2024-01-11 23:12:49
 * @FilePath: \wasm\logic\snake\src\common\utils.go
 * @Description:
 */
package utils

import (
	"math/rand"
	"time"
)

// 索引转为坐标
func Index2Pos(width, index int) (rowIndex, colIndex int) {
	rowIndex = index / width // 行索引，从 0 开始
	colIndex = index % width // 列索引，从 0 开始
	// log.Println("index=", index, " ,row=", rowIndex, " , col=", colIndex)
	return
}

// 坐标转为索引
func Pos2Index(width, rowIndex, colIndex int) (index int) {
	index = rowIndex*width + colIndex
	return
}

var r *rand.Rand

func init() {
	// 设置随机数种子为当前时间戳
	r = rand.New(rand.NewSource(time.Now().UnixNano()))
}

// 随机数
func Rand(num int) int {
	return r.Intn(num)
}
